package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.gamestore.Bean.*;
import com.example.gamestore.mapper.AddressMapper;
import com.example.gamestore.mapper.OrderItemMapper;
import com.example.gamestore.mapper.UserMapper;
import com.example.gamestore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderControl {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/order")
    public String createorder(HttpSession session, Address address){
        Cart cart = (Cart) session.getAttribute("cart");
        User user = (User) session.getAttribute("user");
        Integer account = user.getAccount();
        Integer totalPrice = cart.getTotalPrice();
        //如果余额大于总数
        if(account> totalPrice){
            Integer id = user.getId();
            String orderid = orderService.createOrder(cart, id);
            address.setOrderId(orderid);
            addressMapper.insert(address);
            user.setAccount(account-totalPrice);
            userMapper.updateById(user);
            session.setAttribute("orderid",orderid);
            return "Order/createorder";
        }
        else{
            session.setAttribute("error","账户余额不足,请充值");
            return "Order/createorder";
        }
    }
    @GetMapping("/myorders")
    public String MyOrders(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer id=user.getId();
        List<Orders> ordersList=orderService.selectOrdersById(id);
        model.addAttribute("orderlist",ordersList);
        return "Order/MyOrders";
    }
    @GetMapping("/orderdetails")
    public String OrdersDetails(String orderId,Model model){
        System.out.println(orderId);
        LambdaQueryWrapper<Address> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(Address::getOrderId,orderId);
        Address address = addressMapper.selectOne(wrapper1);
        Orders order=orderService.selectOrderByID(orderId);
        LambdaQueryWrapper<OrderItem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(OrderItem::getOrderId,orderId);
        List<OrderItem> orderItems = orderItemMapper.selectList(wrapper);
        model.addAttribute("address",address)
                .addAttribute("orderItems",orderItems)
                .addAttribute("order",order);
        return "Order/OrdersDetail";
    }
    @GetMapping("/deleteorder")
    @ResponseBody
    public String DeleteOrder(String orderId){
        orderService.deleteOrderById(orderId);
        return "成功";
    }
}
